/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var reqSubmit;
var startStation;
var destinationStation;
var desireTime;
var result_div;
var url;
var resultTable;


function sendRequest(){
    reqSubmit = createRequest();
    generateURL();
    reqSubmit.open("GET", url, true);
    reqSubmit.onreadystatechange = showResult;
    reqSubmit.send(null);
}

function clearResultTable(tableElement) {
    if (tableElement.getElementsByTagName("tr").length > 0) {
        
        for (loop = tableElement.childNodes.length -1; loop >= 0 ; loop--) {
            tableElement.removeChild(tableElement.childNodes[loop]);
        }
    }
}

function showResult(){
    
   
    if (reqSubmit.readyState === 4) {
                    if (reqSubmit.status === 200) {
                        clearResultTable(document.getElementById("resultTable"));
                        resultTable.setAttribute("border", 1);
                        parseResponse(reqSubmit.responseXML);
                    }
    }
    
}

function parseResponse(responseXML) {
    
    // no matches returned
    if (responseXML === null) {
        return false;
    } else {

        var resultpage = responseXML.getElementsByTagName("resultpage")[0];

        if (resultpage.childNodes.length > 0) {
          
            
            for (loop = 0; loop < resultpage.childNodes.length; loop++) {
                var result = resultpage.childNodes[loop];
                var start = result.getElementsByTagName("startStation")[0];
                var destination = result.getElementsByTagName("destinationStation")[0];
                var nextBus = result.getElementsByTagName("nextBus")[0];
                var comesAt = result.getElementsByTagName("comesAt")[0];
                var duration = result.getElementsByTagName("duration")[0];
                appendTable("<strong>Start Station:  </strong>",start.childNodes[0].nodeValue);
                appendTable("<strong>Destination Station:  </strong>",destination.childNodes[0].nodeValue);
                appendTable("<strong>Next Bus:  </strong>", nextBus.childNodes[0].nodeValue);
                appendTable("<strong>Comes at:  </strong>", comesAt.childNodes[0].nodeValue);
                appendTable("<strong>Duration:  </strong>", duration.childNodes[0].nodeValue);
            }
        }
    }
}

function appendTable(rowName, rowValue){
    
    
    row = document.createElement("tr");
    cellName = document.createElement("td");
    cellName.innerHTML = rowName;
    row.appendChild(cellName);
    cellValue = document.createElement("td");
    cellValue.innerHTML = rowValue;
    row.appendChild(cellValue);
    resultTable.appendChild(row);
    
}


function generateURL(){
    url = "searchtime?startStation=" + escape(completeFieldStart.value) + 
          "&destinationStation=" + escape(completeFieldDestination.value) + "&desireTime=" + escape(timeField.value);
  
}


function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (tryMS) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (otherMS) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }	
  return request;
}